import json
from common.flask_package import *
from model.car_info import *
from model.vip import *
from model.order import *
from model.log import *
from common.my_msg import *
from common.my_fun import *
from datetime import datetime,timedelta

bp_order = Blueprint('order', __name__, url_prefix='/order')

# 获取订单列表
@bp_order.route('/get', methods=['GET'])
def order_get():
    if request.method=='GET':
        car_number = request.args.get('car_number')
        page = request.args.get('page', default=1)
        # ''传递过来是一个字符不是空
        if car_number is not None and car_number != '':
            result = Order.order_car_number_search(car_number)
            count = 1
        else:
            result, count = Order.query_order_page_all(page)

        order_list = []
        try:
            for i in result:
                item = {
                    'order_id': i.order_id,
                    'car_number': i.car_number,
                    'order_number': i.order_number,
                    'order_type': i.order_type,
                    'money_sub_time': str(i.money_sub_time),
                    'money': i.money
                }
                order_list.append(item)

            data = {
                'code': state_ok(),
                'msg': '订单信息获取成功',
                'count': count,
                'data': order_list
            }
        except Exception as e:
            data = {
                'code': state_error(),
                'msg': '订单信息获取失败',
                'count': 1,
                'data': order_list
            }

    return Response(json.dumps(data), mimetype='application/json')

# 删除订单
@bp_order.route('/delete', methods=['DELETE'])
def order_delete():
    if request.method=='DELETE':
        order_id=request.args.get('order_id')
        query = Order.query_order_first(order_id)
        relust=Order.del_order(order_id)
        if relust==0:
            Log.add_log('管理员',f'删除【{query.order_number}】订单')
            data={
                'code':state_ok(),
                'msg':'订单删除成功'
            }
        else:
            data = {
                'code': state_error(),
                'msg': '订单删除失败'
            }
    return jsonify(data)


'''
功能：根据车牌号查询订单信息
用途:前台客户端
'''
@bp_order.route('/car/get',methods=['GET'])
def order_car_get():
    if request.method=='GET':
        car_number=request.args.get('car_number') #车牌号
        page=request.args.get('page',default=1)#页数
        num=request.args.get('num',default=10) #每页返回的数据量,默认每页10条数据
        if request.args.get('order_number'):
            order_number = request.args.get('order_number')
            query=Order.order_number_search(car_number,order_number)
            count=1
        else:
            query,count=Order.query_car_number_order(page,num,car_number)
        order_list = []
        if query:
            for i in query:
                item={
                    'order_id':i.order_id,
                    'car_number':i.car_number,
                    'order_number': i.order_number,
                    'order_type': i.order_type,
                    'money_sub_time':f'{i.money_sub_time}',
                    'money':i.money
                }
                order_list.append(item)
            data={
                'code':state_ok(),
                'msg':'订单获取成功',
                'count':count,
                'data':order_list
            }
        else:
            data = {
                'code': state_error(),
                'msg': '未查询到订单',
                'count': count,
                'data': order_list
            }
    return Response(json.dumps(data), mimetype='application/json')

